import { defineStore } from "pinia";
import piniaPersistConfig from "@/config/piniaPersist";
import { UserState } from "../interface";

export const useUserStore = defineStore({
  id: "user",
  state: (): UserState => ({
    token: "",
    userInfo: {}
  }),
  getters: {},
  actions: {
    /**
     * @desc: 设置用户信息
     * @param { UserState } userInfo
     */
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    },
    /**
     * @desc: 设置token
     * @param { string } token
     */
    setToken(token: string) {
      this.token = token;
    },
    /** 登出 */
    async logout() {
      this.resetToken();
    },
    /** 清空token及用户信息 */
    resetToken() {
      this.token = "";
      this.userInfo = {};
    }
  },
  persist: piniaPersistConfig("user")
});
